package mttestclient;

/**
 * Clone of the ClientModel class from the server part of the application used
 * for generating clients and communicating their behavior to the server.
 * Contains an extra field which stores the feedback received from the server.
 * 
 * @author Alex Hrin (0807133)
 */
public class ClientModel {
    private String clientIP;
    private int clientPort;
    private String clientVote = null;
    private String clientSecondVote = null;
    private boolean voteValid = false;
    private boolean voteConfirmed = false;
    private String serverFeedback = "";

    /**
     * Minimal constructor
     * @param clientIP IP address of the connecting client
     * @param clientPort Port over which the client connects
     * @param clientVote The initial vote this client casts
     */
    public ClientModel(String clientIP, int clientPort, String clientVote) {
        this.clientIP = clientIP;
        this.clientPort = clientPort;
        this.clientVote = clientVote;
    }

    /**
     * Getter for the client's IP
     * @return the client's IP address
     */
    public String getClientIP() {
        return clientIP;
    }

    /**
     * Getter for the client's port
     * @return the client's IP
     */
    public int getClientPort() {
        return clientPort;
    }

    /**
     * Getter for the client's initial vote
     * @return the client's initial vote
     */
    public String getClientVote() {
        return clientVote;
    }

    /**
     * Checks if the client's vote has been confirmed (i.e. a second vote 
     * was issued)
     * @return a boolean indicating if the vote was confirmed
     */
    public boolean isVoteConfirmed() {
        return voteConfirmed;
    }

    /**
     * Getter for the client's second vote
     * @return the second vote cast by the client
     */
    public String getClientSecondVote() {
        return clientSecondVote;
    }

    /**
     * Allows the casting of the second vote, raising confirmed and valid flags
     * as needed
     * @param vote the second (confirmation) vote cast by the client
     */
    public void ConfirmVote(String vote) {
        clientSecondVote = vote;
        if (vote.toLowerCase().equals(clientVote.toLowerCase()))
            voteValid = true;
        else
            voteValid = false;
        voteConfirmed = true;
    }

    /**
     * Checks if the vote is valid (both votes have been cast and their values 
     * match)
     * @return a boolean indicating if the vote is valid or not
     */
    public boolean isVoteValid() {
        return voteValid;
    }

    /**
     * Gets the feedback received from the server
     * @return a string where each line represents a response from the server
     */
    public String getServerFeedback() {
        return serverFeedback;
    }

    /**
     * Adds a message to the server feedback string
     * @param serverFeedback the message to be appended to the feedback buffer
     */
    public void appendToServerFeedback(String serverFeedback) {
        this.serverFeedback += "\n" + serverFeedback;
    }
    
}
